<template>
	<view class="form1">
		<base-form v-model="formModel" :labelPosition="'top'" :labelWidth="300" :formList="formList" :btnMessage="btnMessage" @submit="submit"
			v-show="isShow">
		</base-form>
	</view>
</template>

<script>
	import Vue from 'vue'
	import {
		addWorkAPI,
		auditorsAPI
	} from '@/api/public.js'
	import {
		code
	} from '@/api/public.js'
	export default {
		props: ['form', 'pageTyep'],
		data() {
			return {
				jb: null,
				isShow: true,
				fileUrl: '',
				btnMessage: {
					state: false,
					cancels: '上一步',
					submit: '申请',
				},
				formModel: {},
				formList: []
			}
		},
		created() {
			// const level0 = [] // jb 1
			const level1 = ['特级', '一级高处作业', '一级吊装作业', '一级', 'Ⅰ级'] // jb 1
			const level2 = ['二级作业', '二级高处作业', '二级吊装作业', '二级', 'Ⅱ级'] // jb 2
			const level3 = ['三级作业', '三级高处作业', '三级吊装作业', '三级', 'Ⅲ级'] // jb 3
			const level4 = ['四级作业', '四级高处作业', '四级吊装作业', '四级', 'Ⅳ级'] // jb 3
			for (let key in this.form.params) {
				if (level1.includes(this.form.params[key].value)) {
					if (this.pageTyep === "1" && this.form.params[key].value === "一级") {
						this.jb = 2
					} else {
						this.jb = 1
					}
				}
				if (level2.indexOf(this.form.params[key].value) > -1) {
					this.pageTyep === "1" ? this.jb = 3 : this.jb = 2
				}
				if (level3.indexOf(this.form.params[key].value) > -1) {
					this.pageTyep === "1" ? this.jb = 4 : this.jb = 3
				}
				if (level4.indexOf(this.form.params[key].value) > -1) {
					this.pageTyep === "1" ? this.jb = 5 : this.jb = 4
				}
			}

			setTimeout((e)=>{
        this.getHeader()

      },0)
		},
    mounted(){
      let str = JSON.stringify(uni.getStorageSync('ticketData'))
    },
		methods: {
			async getHeader() {
				await auditorsAPI({
					type: this.pageTyep,
					jb: this.jb,
					code: code
				}).then((r) => {
					if (r.code === 200) {
						let msg = r.data.map((e) => {
							return {
								type: "userPicker",
								label: e.ryms,
								model: e.id,
								show: false,
								borderBottom: true,
								placeholder: `请选择${e.ryms}`
							}
						})
						this.formList = msg
						for (let item of this.formList) {
							Vue.set(this.formModel, item.model,'')
						}
					}
				})
				// }
			},
			submit() {
				let a = [];
				let forms = []
				this.form.params.forEach((e, i) => {
					if (e.name !== '监护人' && e.name !== '验收人') {
						if (!!e) {
							forms.push(e)
						}
					}
				})
				console.log('forms', forms);
				for (const key in this.formModel) {
					a.push({
						ry: this.formModel[key],
						rypz: key
					})
				}
				let data = {
					type: this.pageTyep,
					jb: this.jb,
					jsonwbnr: JSON.stringify(forms),
					zymc: this.form.params[0].value,
					list: a,
					zyqy: this.form.params[4].value,
					ysr: this.form.params[1].value,
					jhr: this.form.params[2].value,
					zymc: this.form.params[0].value,
					aqcsIds: this.form.aqcsIds,
					whbsIds: this.form.whbsIds,
					grypfhIds: this.form.grypfhIds,
					orgCode: this.form.orgCode,
					ry: this.form.ry,
					...this.form.qzy,
					// ...this.form
				}
				addWorkAPI(data).then((r) => {
					if (r.code === 200) {
						uni.showToast({
							icon: 'success',
							duration: 2000,
							position: 'top',
							title: `新增成功`
						});
						setTimeout((e) => {
							uni.navigateBack({
								delta: 1
							})
						}, 2000)
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.form1 {
		background: #FFF;

		.signature {
			font-size: 24rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin-top: 60rpx;
			width: 180rpx;
			height: 180rpx;
			background: #F6F6F6;
		}
	}
</style>
